/**
 * runner
 */
package com.whilefor.runner.modules.runner.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.whilefor.runner.common.config.Global;
import com.whilefor.runner.common.persistence.Page;
import com.whilefor.runner.common.web.BaseController;
import com.whilefor.runner.common.utils.StringUtils;
import com.whilefor.runner.modules.runner.entity.RunnerRunner;
import com.whilefor.runner.modules.runner.service.RunnerRunnerService;

/**
 * 跑腿兄弟管理Controller
 * @author lins
 * @version 2016-04
 */
@Controller
@RequestMapping(value = "${adminPath}/runner/runnerRunner")
public class RunnerRunnerController extends BaseController {

	@Autowired
	private RunnerRunnerService runnerRunnerService;
	
	@ModelAttribute
	public RunnerRunner get(@RequestParam(required=false) String id) {
		RunnerRunner entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = runnerRunnerService.get(id);
		}
		if (entity == null){
			entity = new RunnerRunner();
		}
		return entity;
	}
	
	@RequiresPermissions("runner:runnerRunner:view")
	@RequestMapping(value = {"list", ""})
	public String list(RunnerRunner runnerRunner, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RunnerRunner> page = runnerRunnerService.findPage(new Page<RunnerRunner>(request, response), runnerRunner); 
		model.addAttribute("page", page);
		return "modules/runner/runnerRunnerList";
	}

	@RequiresPermissions("runner:runnerRunner:view")
	@RequestMapping(value = "form")
	public String form(RunnerRunner runnerRunner, Model model) {
		model.addAttribute("runnerRunner", runnerRunner);
		return "modules/runner/runnerRunnerForm";
	}

	@RequiresPermissions("runner:runnerRunner:edit")
	@RequestMapping(value = "save")
	public String save(RunnerRunner runnerRunner, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, runnerRunner)){
			return form(runnerRunner, model);
		}
		runnerRunnerService.save(runnerRunner);
		addMessage(redirectAttributes, "保存跑腿兄弟成功");
		return "redirect:"+Global.getAdminPath()+"/runner/runnerRunner/?repage";
	}
	
	@RequiresPermissions("runner:runnerRunner:edit")
	@RequestMapping(value = "delete")
	public String delete(RunnerRunner runnerRunner, RedirectAttributes redirectAttributes) {
		runnerRunnerService.delete(runnerRunner);
		addMessage(redirectAttributes, "删除跑腿兄弟成功");
		return "redirect:"+Global.getAdminPath()+"/runner/runnerRunner/?repage";
	}

}